package com.cmcc.shared.utils

import android.content.Context
import android.util.DisplayMetrics
import android.view.WindowManager

object DisplayUtil {
    /**
     * This method get device specific display metrics,and using the class can get screen size.
     *
     * @param context Context to get system service and device specific display metrics
     * @return DisplayMetrics:device specific display metrics,can get width and height
     */
    fun getScreenSize(context: Context): DisplayMetrics {
        val displayMetrics = DisplayMetrics()
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = wm.defaultDisplay
        display.getMetrics(displayMetrics)
        return displayMetrics
    }

    /**
     * This method get device density,if WindowManager is null, return a default value 1.0.
     *
     * @param context Context to get system service and device specific display metrics
     * @return A float value of device density
     */
    fun getScreenDensity(context: Context): Float {
        val displayMetrics = DisplayMetrics()
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        if (wm != null) {
            val display = wm.defaultDisplay
            display.getMetrics(displayMetrics)
            return displayMetrics.density
        }
        return 1.0f
    }

    /**
     * This method converts dp unit to equivalent pixels, depending on device density.
     *
     * @param dp      A value in dp (density independent pixels) unit. Which we need to convert into pixels
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent px equivalent to dp depending on device density
     */
    fun dp2px(context: Context, dp: Float): Float {
        val resources = context.resources
        val metrics = resources.displayMetrics
        return dp * (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
    }

    /**
     * This method converts device specific pixels to density independent pixels.
     *
     * @param px      A value in px (pixels) unit. Which we need to convert into db
     * @param context Context to get resources and device specific display metrics
     * @return A float value to represent dp equivalent to px value
     */
    fun px2dp(context: Context, px: Float): Float {
        val resources = context.resources
        val metrics = resources.displayMetrics
        return px / (metrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)
    }

    fun sp2px(context: Context, sp: Float): Float {
        val resources = context.resources
        val fontScale = resources.displayMetrics.scaledDensity
        return (sp * fontScale + 0.5f).toInt().toFloat()
    }

    fun px2sp(context: Context, px: Float): Float {
        val resources = context.resources
        val fontScale = resources.displayMetrics.scaledDensity
        return (px / fontScale + 0.5f).toInt().toFloat()
    }

    fun dip2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }
}